package com.rock.app.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.rock.sys.model.TSComp;
import com.rock.sys.model.TSPageComp;

public class CompManager {

	/**
	 * 组装组件数据
	 * 
	 * @param c
	 * @param pageComp
	 * @return
	 */
	public static HashMap<String, Object> buildCompData(OpenControllerBase c, TSPageComp pageComp) {

		HashMap<String, Object> dataMap = new HashMap<String, Object>();

		TSComp comp = pageComp.getComp();
		dataMap.put("compCode", comp.getCompCode());
		dataMap.put("title", pageComp.getTitle());
		dataMap.put("isTitle", pageComp.getIsTitle() == 1 ? true : false);
		dataMap.put("isBorder", pageComp.getIsBorder() == 1 ? true : false);

		String compCode = comp.getCompCode();
		
		//默认将comp_params的值返回给前端
		dataMap.put("compData", JSONObject.parseObject(pageComp.getCompParams()));

		// 自定义宫格
		if (TSComp.COMP_DIY_NAV_CELL.equals(compCode)) {
			List<HashMap<String, String>> map = getDiyNavData(c, pageComp);
			dataMap.put("compData", map);
		}

		return dataMap;
	}



	/**
	 * 自定义宫格组件数据
	 * 
	 * @param c
	 * @param pageComp
	 * @return
	 */
	public static List<HashMap<String, String>> getDiyNavData(OpenControllerBase c, TSPageComp pageComp) {
		JSONObject compParams = JSONObject.parseObject(pageComp.getCompParams());
		// 查询所有分组轮播图
		List<Record> navs = Db.find("select name,imgsrc,url from t_ap_diynav where tag=? order by sort_no", compParams.get("tag"));

		List<HashMap<String, String>> mapList = new ArrayList<HashMap<String, String>>();
		for (Record nav : navs) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", nav.getStr("name"));
			map.put("imgsrc", nav.getStr("imgsrc"));
			map.put("url", nav.getStr("url"));
			mapList.add(map);
		}
		return mapList;

	}

}
